package com.meida.module.pay.provider.service.impl;

import com.meida.common.mybatis.base.service.impl.BaseServiceImpl;
import com.meida.common.base.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.utils.ApiAssert;
import com.meida.module.pay.client.entity.PayCashout;
import com.meida.module.pay.client.entity.PayConfig;
import com.meida.module.pay.provider.mapper.PayCashoutMapper;
import com.meida.module.pay.provider.service.PayBalanceLogService;
import com.meida.module.pay.provider.service.PayBalanceService;
import com.meida.module.pay.provider.service.PayCashoutService;
import com.meida.module.pay.provider.service.PayConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 提现申请表 实现类
 *
 * @author flyme
 * @date 2019-12-20
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PayCashoutServiceImpl extends BaseServiceImpl<PayCashoutMapper, PayCashout> implements PayCashoutService {

    @Resource
    private PayBalanceService balanceService;
    @Resource
    private PayBalanceLogService balanceLogService;
    @Resource
    private PayConfigService payConfigService;

    @Override
    public ResultBody cashoutLogs(Map map) {
        CriteriaQuery<PayCashout> cq = new CriteriaQuery(map, PayCashout.class);
        EntityMap requestMap = cq.getRequestMap();
        cq.select("cashout.*");
        cq.eq("cashout.userId",requestMap.get("userId"));
        cq.eq("cashout.userType",requestMap.get("userType"));
        cq.eq("cashout.cashoutType",requestMap.get("cashoutType"));
        cq.eq("cashout.status",requestMap.get("status"));
        cq.orderByDesc("cashout.createTime");
        ResultBody resultBody = basePageList(cq);
        return resultBody;
    }

    @Override
    public ResultBody cashoutAdd(PayCashout cashout, Integer balanceLogType, String title, String body,String payPwd) {
        PayConfig payConfig = payConfigService.getById(cashout.getPayId());
        cashout.setCashoutType(payConfig.getPayType().name());
        boolean insert = cashout.insert();
        if (insert){
            boolean b = balanceService.subtractBalance(cashout.getUserId(), cashout.getUserType(), cashout.getMoney(), null, balanceLogType, cashout.getCashoutId(), title, body, 0,payPwd);
            if (!b){
                ApiAssert.failure("系统繁忙，请稍后再试！");
            }
        }
        return ResultBody.result("提交",insert);
    }
}
